const workerpool = require('workerpool');
const XLSX = require('xlsx')
const path = require('path')
const { folder2, workArea } = require('./config.js');
const worker = (taskData) => {
    const { data, taskId } = taskData;
    const folderPath = path.join(__dirname, ...folder2.split('/'));
    let SheetData = []
    let PoData = []
    let DwData = []
    data.map((fileName, i) => {
        // console.log(`${fileName}文件读取中---`);
        let file = ''
        try {
            file = XLSX.readFile(`${folderPath}/${fileName}`)
        } catch (error) {
            console.log(`${fileName}加密文件:${i + 1}`);
            return undefined
        }
        workArea.map((v, idx) => {
            v.map((val) => {
                const worksheet = file.Sheets[val];
                if (!worksheet) return undefined
                const data = XLSX.utils.sheet_to_json(worksheet, {
                    defval: "" // 空单元格返回空字符串，而不是 undefined
                });
                data.forEach((v) => {
                    const A1 = Object.keys(v)[0]
                    const B1 = Object.keys(v)[1]
                    const idCardRegex = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/
                    if (idCardRegex.test(v[B1])) {
                        if (idx == 0) {
                            SheetData.push({ '名字': v[A1], '身份证号': v[B1] })
                        }
                        if (idx == 1) {
                            PoData.push({ '名字': v[A1], '身份证号': v[B1] })
                        }
                        if (idx == 2) {
                            DwData.push({ '名字': v[A1], '身份证号': v[B1] })
                        }
                    }
                })
            })
        })
    })
    // 添加工作表并创建文件
    const workbook = XLSX.utils.book_new();
    const toplimit = 1048575
    if (SheetData.length < toplimit) {
        SheetData = Array.from(new Map(SheetData.map(item => [item['身份证号'], item])).values());
        const Sheet = XLSX.utils.json_to_sheet(SheetData);
        XLSX.utils.book_append_sheet(workbook, Sheet, `${workArea[0][0]}`);
    } else {
        let result = []
        SheetData = Array.from(new Map(SheetData.map(item => [item['身份证号'], item])).values());
        for (let i = 0; i < SheetData.length; i += toplimit) {
            result.push(SheetData.slice(i, i + toplimit));
        }
        SheetData = []
        for (let sheetIndex = 0; sheetIndex < result.length; sheetIndex++) {
            const Sheet = XLSX.utils.json_to_sheet(result[sheetIndex]);
            XLSX.utils.book_append_sheet(workbook, Sheet, workArea[0][0] + (sheetIndex + 1));
        }
        XLSX.writeFile(workbook, `${folderPath}/../汇总/数据${1 + ((taskId - 1) * 100)}-${100 + ((taskId - 1) * 100)}.xlsx`);
    }
    if (PoData.length) {
        PoData = Array.from(new Map(PoData.map(item => [item['身份证号'], item])).values());
        const PO = XLSX.utils.json_to_sheet(PoData);
        XLSX.utils.book_append_sheet(workbook, PO, workArea[1][0]);
    }
    if (DwData.length) {
        DwData = Array.from(new Map(DwData.map(item => [item['身份证号'], item])).values());
        const DW = XLSX.utils.json_to_sheet(DwData);
        XLSX.utils.book_append_sheet(workbook, DW, workArea[2][0]);
    }
    if (SheetData.length || PoData.length || DwData.length) {
        XLSX.writeFile(workbook, `${folderPath}/../汇总/数据${1 + ((taskId - 1) * 100)}-${100 + ((taskId - 1) * 100)}.xlsx`);
    }
}
workerpool.worker({
    worker
});